<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>工厂方法</title>
</head>
<body>
<script>
    //抽象的产品计算 规则
    class Calculation{
        //功能
        toDo(){
            console.log("某某方法能进行数学计算")
        }
    }

    //具体产品
    class Add extends Calculation{
        toDo(x,y) {
            console.log(`能进行加法运算:${x}+${y}=${x+y}`)
        }
    }

    class Minus extends Calculation{
        toDo(x,y) {
            console.log(`能进行减法运算:${x}-${y}=${x-y}`)
        }
    }

    class Multiply extends Calculation{
        toDo(x,y) {
            console.log(`能进行乘法运算:${x}*${y}=${x*y}`)
        }
    }

    class Divide extends Calculation{
        toDo(x,y) {
            console.log(`能进行除法运算:${x}/${y}=${x/y}`)
        }
    }

    //抽象工厂
    class CalculationFactory{
        createCalculation(){
            console.log("某某工厂生产某某运算")
        }
    }

    //具体工厂
    class AddFactory extends CalculationFactory{
        createCalculation() {
            console.log("加法工厂生产加法运算")
            return new Add()
        }
    }

    class MinusFactory extends CalculationFactory{
        createCalculation() {
            console.log("减法工厂生产减法运算")
            return new Minus()
        }
    }

    class MultiplyFactory extends CalculationFactory{
        createCalculation() {
            console.log("乘法工厂生产乘法运算")
            return new Multiply()
        }
    }

    class DivideFactory extends CalculationFactory{
        createCalculation() {
            console.log("除法工厂生产除法运算")
            return new Divide()
        }
    }

    let myAdd = new AddFactory()
    myAdd.createCalculation().toDo(6,6)

    let myMinus = new MinusFactory()
    myMinus.createCalculation().toDo(6,6)

    let myMultiply = new MultiplyFactory()
    myMultiply.createCalculation().toDo(6,6)

    let myDivide = new DivideFactory()
    myDivide.createCalculation().toDo(6,6)
</script>
</body>
</html>